ggplot2(7)-注解

添加文本注解

添加文本注解的函数是annotate(),查看数据集

1
2
3
4
5
6
7
8
> head(faithful)
eruptions waiting
1 3.600 79
2 1.800 54
3 3.333 74
4 2.283 62
5 4.533 85
6 2.883 55

绘图,如下所示:

1
2
3
4
library(ggplot2)
p <- ggplot(faithful,aes(x=eruptions,y=waiting))+geom_point()
p + annotate("text",x=3,y=48,label="Group 1")+
annotate("text",x=4.5,y=66,label="Group 2")

mark

对注解进行修饰

1
2
3
4
p + annotate("text",x=3,y=48,label="Group 1",family="serif",
fontface="italic",colour="darkred",size=3)+
annotate("text",x=4.5,y=66,label="Group 2", family="serif",
fontface="italic",colour="darkred",size=3)

mark

在添加文本的时候,不要用geom_text() ,如下所示:

1
2
p + annotate("text",x=3,y=48, label="Group 1",alpha=0.1)+
geom_text(x=4.5,y=61,label="Group 2",alpha=0.1)

mark

如果坐标轴是连续型的,可以使用Inf和-Inf在绘图区域的边放置文本注解。同时用vjust与hjust来进行微调。

1
2
p + annotate("text",x=-Inf,y=Inf,label="Upper left",hjust=-0.2,vjust=2)+
annotate("text",x=mean(range(faithful$eruptions)),y=-Inf,vjust=-0.4,label="Bottom middle")

mark

使用数学表达式

1
2
p <- ggplot(data.frame(x=c(-3,3)),aes(x=x))+stat_function(fun=dnorm)
p + annotate("text",x=2,y=0.3,parse=TRUE,label="frac(1,sqrt(2*pi))*e^{-x^2/2}")

mark

如果要显示两个相邻的变量,需要在它们之间放置一个星号(*),如下所示:

1
2
p + annotate("text",x=0,y=0.05,parse=TRUE,size=4,
label="'Function:'*y==frac(1,sqrt(2*pi))*e^{-x^2/2}")

mark

添加直线

横线:geom_hline(),即h对应的是horizon 竖线:geom_vline(),即v对应的是vertical

1
2
3
library(gcookbook)
p <- ggplot(heightweight,aes(x=ageYear,y=heightIn,colour=sex))+geom_point()
p + geom_hline(yintercept=60)+geom_vline(xintercept=14) # 添加横线与竖线

mark

以下是添加有角度的直线

1
p + geom_abline(intercept=37.4,slop=1.75)

mark

将数据映射到yintercept上

1
2
3
4
5
6
7
8
9
library(plyr)
hw_means <- ddply(heightweight,"sex",summarise,heightIn=mean(heightIn))
# 这一段话的意思是,对heightweight的数据集进行处理,将其按照sex进行分类,增加一列为heightIn,heightIn的值是分类后原数据集中heightIn的均值
hw_means
## sex heightIn
## 1 f 60.52613
## 2 m 62.06000
p + geom_hline(aes(yintercept=heightIn,colour=sex),data=hw_means,linetype="dashed",size=1)

mark

当数据是离散型

下面的两个图形中,坐标轴中有ctrl,trt1,trt2三个标签,分别是因子水平1,2,3

1
2
3
4
5
6
7
8
9
10
11
12
head(PlantGrowth)
## weight group
## 1 4.17 ctrl
## 2 5.58 ctrl
## 3 5.18 ctrl
## 4 6.11 ctrl
## 5 4.50 ctrl
## 6 4.61 ctrl
pg <- ggplot(PlantGrowth,aes(x=group,y=weight))+geom_point()
pg

mark

添加线段与箭头

1
2
3
p <- ggplot(subset(climate,Source=="Berkeley"),aes(x=Year,y=Anomaly10y))+
geom_line()
p + annotate("segment",x=1950,xend=1980,y=-0.25,yend=-0.25)

mark

添加箭头

用grid包中的arrow()函数

1
2
3
4
5
library(grid)
p + annotate("segment",x=1850,xend=1820,y=-0.8,yend=-0.95,colour="blue",size=2,arrow=arrow())+
annotate("segment",x=1950,xend=1980,y=-0.25,yend=-0.25,
arrow=arrow(ends="both",angle=90,length=unit(0.2,"cm")))
# 参数angle是角度,length是设置长度,默认的是英寸

mark

添加矩形阴影

1
2
p <-ggplot(subset(climate,Source=="Berkeley"),aes(x=Year,y=Anomaly10y))+geom_line()
p+annotate("rect",xmin=1950,xmax=1980,ymin=-1,ymax=1,alpha=0.1,fill="blue")

mark

上图是先添加曲线,然后加入面积,面积在曲线的上层,如果改变添加顺序,可以调整图形的叠加:

下面的例子是把线添加到矩形上面:

1
p + annotate("rect",xmin=1950,xmax=1980,ymin=-1,ymax=1,alpha=1,fill="blue")+geom_line()

mark

下面的图形是把矩形放到线上面:

1
p + geom_line()+annotate("rect",xmin=1950,xmax=1980,ymin=-1,ymax=1,alpha=1,fill="blue")

mark

高亮某一元素

1
2
3
4
5
6
pg <- PlantGrowth
pg$h1 <- "no" # 添加一列h1,全都为no
pg$h1[pg$group=="trt2"] <- "yes"
# 把pg的grouop中是trt2的改为yes
ggplot(pg,aes(x=group,y=weight,fill=h1))+geom_boxplot()+
scale_fill_manual(values=c("grey85","#FFDDCC"),guide=FALSE)

mark

在上述案例中,可以将group映射给fill,得到的结果相同:

1
2
ggplot(PlantGrowth,aes(x=group,y=weight,fill=group))+geom_boxplot()+
scale_fill_manual(values=c("grey85","grey85","#FFDDCC"),guide=FALSE)

mark

添加误差线

1
2
ce <- subset(cabbage_exp,Cultivar=="c39")
ggplot(ce,aes(x=Date,y=Weight))+ geom_bar(fill="white",colour="black",stat = "identity")+geom_errorbar(aes(ymin=Weight-se,ymax=Weight+se),width=0.2)

mark

为折线图添加误差线:

1
2
3
4
ggplot(ce,aes(x=Date,y=Weight))+
geom_line(aes(group=1))+
geom_point(size=4)+
geom_errorbar(aes(ymin=Weight-se,ymax=Weight+se),width=0.2)

mark

条形图误差线

条形图的修改 默认情况下,geom_bar()的并列宽度是0.9,如果在geom_errorbar()中不进行指定,则误差线不会与条形图正中央对齐,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
head(cabbage_exp)
## Cultivar Date Weight sd n se
## 1 c39 d16 3.18 0.9566144 10 0.30250803
## 2 c39 d20 2.80 0.2788867 10 0.08819171
## 3 c39 d21 2.74 0.9834181 10 0.31098410
## 4 c52 d16 2.26 0.4452215 10 0.14079141
## 5 c52 d20 3.11 0.7908505 10 0.25008887
## 6 c52 d21 1.47 0.2110819 10 0.06674995
ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+
geom_bar(position="dodge",stat="identity")+
geom_errorbar(aes(ymin=Weight-se,ymax=Weight+se),position="dodge",width=0.2)

mark

如果在geom_errorbar()中进行了设置,即position=positon_dodge(0.9),则误差线则与条形图中央进行对齐:

1
2
3
4
ggplot(cabbage_exp,aes(x=Date,y=Weight,fill=Cultivar))+
geom_bar(position="dodge",stat="identity")+
geom_errorbar(aes(ymin=Weight-se,ymax=Weight+se),
position=position_dodge(0.9),width=0.2)

mark

误差线的调节

如果误差线与线和点的颜色不同,则应先绘制误差线,再绘制线,这样误差线就在线的下层:

1
2
3
4
5
6
7
pd <- position_dodge(0.3)
ggplot(cabbage_exp,aes(x=Date,y=Weight,colour=Cultivar,group=Cultivar))+
geom_errorbar(aes(ymin=Weight-se,ymax=Weight+se),
width=0.2,size=0.25,colour="black",position=pd)+
geom_line(position=pd)+
geom_point(position=pd,size=2.5)

mark

参考资料

  1. 常肖楠, 邓一硕, 魏太云. R数据可视化手册[M]. 人民邮电出版社, 2014.